/**
 * 
 * 起迪科技 Copyright (c) 2014-2018 QiDi,Inc.All Rights Reserved.
 */
package cn.qidisoft.edu.hzjt.service.impl;

import java.io.File;
import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.qidisoft.core.common.code.QdfRespCode;
import cn.qidisoft.core.common.exception.dao.DBException;
import cn.qidisoft.core.common.log.LoggerAdapter;
import cn.qidisoft.core.common.log.LoggerAdapterFactory;
import cn.qidisoft.core.common.pagination.Page;
import cn.qidisoft.core.common.pagination.PaginationBean;
import cn.qidisoft.core.util.uuid.UUIDGenerator;
import cn.qidisoft.edu.hzjt.constants.Constant;
import cn.qidisoft.edu.hzjt.dao.CommunityAmateurHomeworkAttMapper;
import cn.qidisoft.edu.hzjt.dao.CommunityAmateurHomeworkMapper;
import cn.qidisoft.edu.hzjt.model.CommunityAmateurHomework;
import cn.qidisoft.edu.hzjt.model.CommunityAmateurHomeworkAtt;
import cn.qidisoft.edu.hzjt.service.ICommunityAmateurHomeworkService;
import cn.qidisoft.edu.hzjt.utils.FileTool;
import cn.qidisoft.edu.hzjt.utils.LogType;

/**
 * 
 * 
 * @author Administrator
 * @version $Id: BjxxClassMaterialProjectServiceImpl.java, v 0.1 2018年10月29日 下午1:14:36 hhl Exp $
 */
@Service("communityAmateurHomeworkService")
public class CommunityAmateurHomeworkServiceImpl implements ICommunityAmateurHomeworkService {

    protected static final LoggerAdapter      LOGGER = LoggerAdapterFactory
        .getLogger(LogType.HZJT_LOGGER.val);

    @Autowired
    private CommunityAmateurHomeworkMapper    communityAmateurHomeworkMapper;

    @Autowired
    private CommunityAmateurHomeworkAttMapper communityAmateurHomeworkAttMapper;

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#queryList(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    public List<CommunityAmateurHomework> queryList(CommunityAmateurHomework data) {
        try {
            return communityAmateurHomeworkMapper.queryList(data);
        } catch (Exception e) {
            LOGGER.error("queryList [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#queryByPK(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    public CommunityAmateurHomework queryByPK(CommunityAmateurHomework data) {
        try {
            return communityAmateurHomeworkMapper.queryByPK(data);
        } catch (Exception e) {
            LOGGER.error("queryByPK [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#add(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    @Transactional
    public String add(CommunityAmateurHomework data) {
        String uuid = UUIDGenerator.genReqID();
        try {
            communityAmateurHomeworkMapper.add(data);

        } catch (Exception e) {
            LOGGER.error("add [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
        return uuid;
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#update(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    @Transactional
    public void update(CommunityAmateurHomework data) {
        try {

            communityAmateurHomeworkMapper.update(data);

        } catch (Exception e) {
            LOGGER.error("update [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#delete(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    @Transactional
    public void delete(CommunityAmateurHomework data) {
        try {
            for (String s : data.getIds()) {
                CommunityAmateurHomeworkAtt data2 = new CommunityAmateurHomeworkAtt();
                data2.setParentid(Integer.valueOf(s));
                List<CommunityAmateurHomeworkAtt> communityAmateurHomeworkAttList = communityAmateurHomeworkAttMapper
                    .queryList(data2);
                if (communityAmateurHomeworkAttList != null
                    && !communityAmateurHomeworkAttList.isEmpty()) {
                    for (CommunityAmateurHomeworkAtt r : communityAmateurHomeworkAttList) {
                        String upload = Constant.UPLOADPATH;
                        int idlength = r.getFileid().lastIndexOf("");
                        int pos = r.getFilepath().lastIndexOf(".");
                        String filepath = r.getFilepath().substring(0, pos - idlength - 1);
                        File file = new File(upload + File.separator + filepath);
                        /* if (file.isDirectory()) {//判断file是否是文件目录 若是返回TRUE  
                            String name[] = file.list();//name存储file文件夹中的文件名  
                            for (int i = 0; i < name.length; i++) {
                                File f = new File(upload + File.separator + filepath, name[i]);//此时就可得到文件夹中的文件  
                                f.delete();//删除文件  
                            }
                            FileUtils.forceDelete(new File(upload + File.separator + filepath)); //最后删除文件目录
                        }*/
                        FileTool.deleteFile(file);
                    }
                }
                //删除附件
                CommunityAmateurHomeworkAtt communityAmateurHomeworkAtt = new CommunityAmateurHomeworkAtt();
                communityAmateurHomeworkAtt.setParentid(Integer.valueOf(s));
                communityAmateurHomeworkAttMapper.delete(communityAmateurHomeworkAtt);
                //删除主表
                data.setZyid(Integer.valueOf(s));
                communityAmateurHomeworkMapper.delete(data);
            }

        } catch (Exception e) {
            LOGGER.error("delete [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#queryPageList(cn.qidisoft.microClass.resource.model.basis.Catalog, cn.qidisoft.core.common.pagination.Page)
     */
    @Override
    public PaginationBean<CommunityAmateurHomework> queryPageList(CommunityAmateurHomework data,
                                                                  Page page) {
        try {

            int c = communityAmateurHomeworkMapper.queryPageListcount(data);
            int totalRecords = 0;
            totalRecords = (int) c;
            PaginationBean<CommunityAmateurHomework> pageHolder = new PaginationBean<CommunityAmateurHomework>(
                page, totalRecords);
            List<CommunityAmateurHomework> resultList = communityAmateurHomeworkMapper
                .queryPageList(data,
                    new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                        pageHolder.getPageSize()));
            pageHolder.setPageList(resultList);
            return pageHolder;
        } catch (Exception e) {
            LOGGER.error("queryPageList [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#validation(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    public int validation(CommunityAmateurHomework data) {
        try {
            return communityAmateurHomeworkMapper.validation(data);
        } catch (Exception e) {
            e.printStackTrace();
            return -99;
        }
    }

}
